package com.zpark.controller;

import com.zpark.entity.Books;
import com.zpark.mapper.IBooksMapper;
import com.zpark.service.IBooksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @PackageName: com.zpark.controller
 * @ClassName: BooksController
 * @Description:
 * @author: RZS
 * @date: 2023/5/8  16:02
 */
@RestController
@RequestMapping("/books")
// 设置允许跨域访问
@CrossOrigin
public class BooksController {

    @Autowired
    private IBooksService booksService;

    // 添加books数据
    @RequestMapping("/add")
    public Integer addBooks(@RequestBody Books books) {
        System.out.println(books);
        return booksService.addBooks(books);
    }

    // 根据id删除books
    @RequestMapping("/del/{id}")
    public Integer delBooksById(@PathVariable("id") Integer id){
        return booksService.delBooksById(id);
    }

    // 修改books
    @RequestMapping("/update")
    public Integer changeBooksById(@RequestBody Books books){
        System.out.println(books);
        return booksService.changeBooksById(books);
    }

    // 查询所有books
    @GetMapping("/find_all")
    public List<Books> findAll(){
        return booksService.findAll();
    }

    // 根据books名字模糊查询books
    @GetMapping("/find_by_name/{name}")
    public List<Books> findBooksByLikeName(@PathVariable("name") String bName){
        return booksService.findBooksByLikeName(bName);
    }

    // 根据ID查询book
    @GetMapping("/find_by_id/{id}")
    public Books findBooksById(@PathVariable("id") Integer id){
        return booksService.findById(id);
    }

}
